﻿using GCC.Container.Abstracts;

namespace GCC.APIContainer
{
    /// <summary>
    /// 容器上下文
    /// </summary>
    public partial class GCCContext : AbsGCCContext
    {
        internal static string _authHeadKey = "";

        /// <summary>
        /// 根据Token获取用户
        /// </summary>
        /// <param name="token">token</param> 
        /// <returns>用户信息</returns>
        public override async Task<GCCUserInfo?> GetUserByToken(string token)
        {
            var res = await System.AuthCenter.Client.Manager.GetUserByToken(new GCCGrpc.AuthCenter.GetUserByTokenRequest()
            {
                AccessToken = token,
                Token = token
            }, EventID);
            if (res is null || res.Uid == 0)
            {
                return null;
            }
            return new GCCUserInfo()
            {
                Roles = res.Roles.ToList(),
                UID = res.Uid,
                Scopes = res.Scopes.ToList(),
            };
        }
        /// <summary>
        /// 获取用户的所有登陆信息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public override async Task<List<GCCUserLoginInfos>> GetUserLoginInfos(long uid)
        {
            var res = await System.AuthCenter.Client.Manager.GetUserLoginInfos(new GCCGrpc.AuthCenter.GetUserLoginInfosRequest()
            {
                UID = uid,
                AccessToken = ""
            }, EventID);
            if (res is null)
            {
                return new();
            }
            return res.LoginInfoItems.Select(item => new GCCUserLoginInfos()
            {
                Created = new DateTime(item.Created),
                CreatedBy = item.CreatedBy,
                Modified = new DateTime(item.Modified),
                ModifiedBy = item.ModifiedBy,
                OpenID = item.OpenID,
                OpenKey = item.OpenKey,
                RefreshToken = item.RefreshToken,
                Token = item.Token,
                Type = item.Type,
                UID = item.UID,
                UnionID = item.UnionID,
            }).ToList();
        }
        /// <summary>
        /// 设置用户信息,如果不存在则新增
        /// </summary>
        /// <param name="request">登陆信息</param>
        /// <param name="accessToken">当前人员的访问Token</param>
        /// <returns></returns>
        public override async Task<bool> SetUser(GCCUserLoginInfos request, string accessToken)
        {
            var res = await System.AuthCenter.Client.Manager.SetUser(new GCCGrpc.AuthCenter.SetUserRequest()
            {
                AccessToken = accessToken,
                OpenID = request.OpenID,
                OpenKey = request.OpenKey,
                RefreshToken = request.RefreshToken,
                Token = request.Token,
                Type = request.Type,
                UID = request.UID,
                UnionID = request.UnionID,
            }, EventID);
            if (res is null)
            {
                return new();
            }
            return res.Completed;
        }
        /// <summary>
        /// 设置用户的角色
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="roles">要设置得角色ID列表</param>
        /// <param name="accessToken">当前人员的访问Token</param>
        /// <returns></returns>
        public override async Task<bool> SetUserRoles(long uid, List<long> roles, string accessToken)
        {
            var request = new GCCGrpc.AuthCenter.SetUserRolesRequest()
            {
                AccessToken = accessToken,
                UID = uid,
            };
            roles.ForEach(item =>
            {
                request.RoleID.Add(item);
            });
            var res = await System.AuthCenter.Client.Manager.SetUserRoles(request, EventID);
            if (res is null)
            {
                return new();
            }
            return res.Completed;
        }
        /// <summary>
        /// 获取用户的所有角色
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public override async Task<List<GCCRoles>> GetUserRoles(long uid)
        {
            var res = await System.AuthCenter.Client.Manager.GetUserRoles(new GCCGrpc.AuthCenter.GetUserRolesRequest()
            {
                UID = uid,
                AccessToken = ""
            }, EventID);
            if (res is null)
            {
                return new();
            }
            return res.Roles.Select(item => new GCCRoles()
            {
                Name = item.Name,
                RoleID = item.RoleID,
            }).ToList();
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public override async Task<string?> Login(GCCLoginRequest request)
        {
            var res = await System.AuthCenter.Client.Manager.Login(new GCCGrpc.AuthCenter.LoginRequest()
            {
                AccessToken = "",
                OpenID = request.OpenID,
                OpenKey = request.OpenKey,
                RefreshToken = request.RefreshToken,
                Token = request.Token,
                Type = request.Type,
                UnionID = request.UnionID,

            }, EventID);
            if (res is null)
            {
                return null;
            }
            return res.Token;
        }

        /// <summary>
        /// 设置用户的区域信息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="scopes">要设置得角色ID列表</param>
        /// <param name="accessToken">当前人员的访问Token</param>
        /// <returns></returns>
        public override async Task<bool> SetUserScopes(long uid, List<long> scopes, string accessToken)
        {
            var request = new GCCGrpc.AuthCenter.SetUserScopesRequest()
            {
                AccessToken = accessToken,
                UID = uid,
            };
            scopes.ForEach(item =>
            {
                request.ScopesID.Add(item);
            });
            var res = await System.AuthCenter.Client.Manager.SetUserScopes(request, EventID);
            if (res is null)
            {
                return new();
            }
            return res.Completed;
        }
        /// <summary>
        /// 获取用户的所有角色
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public override async Task<List<GCCScopes>> GetUserScopes(long uid)
        {
            var res = await System.AuthCenter.Client.Manager.GetUserScopes(new GCCGrpc.AuthCenter.GetUserScopesRequest()
            {
                UID = uid,
                AccessToken = ""
            }, EventID);
            if (res is null)
            {
                return new();
            }
            return res.Scopes.Select(item => new GCCScopes()
            {
                Name = item.Name,
                RoleID = item.ScopesID,
            }).ToList();
        }

        /// <summary>
        /// 获取当前的认证的Token头
        /// </summary> 
        /// <returns>头</returns>
        public override string GetAuthHeaderKey() => _authHeadKey;



        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="token">token</param>
        /// <returns></returns>
        public override async Task Logout(string token)
        {
            _ = await System.AuthCenter.Client.Manager.Logout(new GCCGrpc.AuthCenter.LogoutRequest()
            {
                Token = token
            }, EventID);

        }

    }
}
